package com.zzb.framework.log.service;

import com.alibaba.fastjson.JSON;
import com.zzb.framework.log.dao.SysLogDAO;
import com.zzb.framework.log.entity.SysLog;
import com.zzb.framework.log.enums.LogLevelEnum;
import com.zzb.framework.role.entity.IUser;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.BaseService;
import com.zzb.framework.common.ServiceException;
import com.zzb.framework.user.utils.SystemUser;
import com.zzb.framework.validator.annotation.ValidatorEnabled;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 系统日志 service类
*/
@Service("sysLogService")
public class SysLogService extends BaseService<SysLogDAO,SysLog> {
    private static final Logger logger = LoggerFactory.getLogger(SysLogService.class);
    @Autowired
    private SysLogDAO sysLogDAO;
    @Override
    public SysLogDAO getDAO() {
        return sysLogDAO;
    }
    /**删除系统日志*/
    public BaseResponse deleteSysLog(String id,IUser operator) throws ServiceException{
        return deleteById(id);
    }
    /**保存系统日志*/
    @ValidatorEnabled
    public BaseResponse<SysLog> saveSysLog(SysLog sysLog, IUser operator) throws ServiceException{
        logger.info("正在保存系统日志["+ JSON.toJSONString(sysLog)+"]["+JSON.toJSONString(operator)+"]");
        return insertOrUpdate(sysLog,operator);
    }

    @Transactional
    public void testTransaction() throws ServiceException{
        SysLog sysLog = new SysLog();
        sysLog.setClassName("this");
        sysLog.setLineNumber(1);
        sysLog.setLogContent("test");
        sysLog.setLogLevel(LogLevelEnum.INFO.name());
        sysLog.setMethodName("test()");
        sysLog.setLogType("1");
        saveSysLog(sysLog, SystemUser.root());
        String id = sysLog.getId();
        SysLog mybatisEntity = getDAO().getMapper().get(id);
        if(mybatisEntity == null){
            throw new ServiceException("测试失败");
        }
    }
}
